<template>
    <el-drawer
      title="图片选择器（数据来源：资源管理）"
      :visible.sync="visible"
      :direction="direction"
      :modal="false"
      :before-close="closeDrawer"
    >
        <div class="groupSelector">
            <el-form-item>
                <el-select v-model="thisGroup" placeholder="请选择分组" filterable @change="changeGroup">
                    <el-option v-for="i in groups" :key="i.uuid" :label="i.groupName" :value="i.uuid"></el-option>
                </el-select>
            </el-form-item>
        </div>
        <div class="image" v-for="image in images" :key="image.uuid" @click="changeImage(image.resourceUrl)">
            <img :src="image.resourceUrl" alt="" :title="image.resourceName">
        </div>
        <div class="image upload-image" @click="uploadImage">
            <i class="el-icon-plus"></i>
            <input id="uploadImage" type="file" @change="fileChange" accept=".jpg, .jpeg, .png">
        </div>
    </el-drawer>
</template>
  
<script>
export default {
    props: ["visible", "value"],
    data() {
      return {
        // 可用图标，后期使用字典配置代替
        images: [],
        direction: "rtl",
        thisGroup: undefined,
        groups:[],
      };
    },
    mounted(){
        this.getGroupList();
        this.getImages(this.thisGroup);
    },
    methods: {
        changeGroup(){
            this.getImages(this.thisGroup);
        },
        // 获取分组列表
        getGroupList() {
            this.groupLoading = true;
            this.groups = [
                {
                    uuid: undefined,
                    groupName: "全部资源"
                }
            ]
            this.$api.resource.getGroupList().then(res => {
                if (res.code == 200 && res.data != null) {
                    res.data.forEach(e => {
                        this.groups.push(e);
                    });
                }
                this.groupLoading = false;
            })
        },
        fileChange(event){
            let files = event.target.files || event.dataTransfer.files;
            if(files && files.length > 0){
                let file = files[0];
                const formData = new FormData();
                formData.append("file", file);
                formData.append("groupName",this.thisGroup);
                this.$api.resource.uploadFile(formData).then(res=>{
                    if(res.code == 200){
                        this.$message.success("上传成功!");
                        this.getImages(this.thisGroup);
                    }else{
                        this.$message.error(res.msg);
                    }
                });
            }
        },
        uploadImage(){
            let fileInput = document.querySelector("#uploadImage");
            fileInput.click();
        },
        getImages(groupName){
            this.$api.resource.getPicList({groupName:groupName}).then(res => {
                if(res.code == 200){
                    this.images = res.data.list;
                }else{
                    this.$message.error(res.msg);
                }
            })
        },
        changeImage(resourceUrl){
            this.$emit('update:value', resourceUrl)
        },
        closeDrawer() {
            this.radio = "";
            this.$emit('update:visible', false)
        },
    },
};
</script>
  
<style lang="less" scoped>
.groupSelector{
    padding: 10px;
}
.image{
    float: left;
    width: 62px;
    height: 62px;
    margin: 10px;
    border: 1px dashed #d9d9d9;
    cursor: pointer;
    border-radius: 6px;
    img{
        max-width: 60px;
        max-height: 60px;
    }
}
.image:hover{
    border-color: #409eff;
}
.upload-image{
    font-size: 28px;
    color: #8c939d;
    line-height: 62px;
    text-align: center;
    input{
        display: none;
    }
}
</style>